﻿<template>
    <el-card class='!border-none mb-4' shadow='never'>
        <popup
            ref='popupRef'
            :title='popupTitle'
            :async='true'
            width='580px'
            @confirm='handleSubmit'
            @close='handleClose'
        >
            <el-form ref='formRef' :model='formData' label-width='90px' :rules='formRules'>
                <el-form-item label='标题' prop='title'>
                    <el-input v-model='formData.title' clearable placeholder='请输入标题' />
                </el-form-item>
                <el-form-item label='图片' prop='banner_image'>
                    <material-picker size='260px' :limit='1' v-model='formData.banner_image' />
                </el-form-item>
                <el-form-item label='绑定文章' prop='article_id'>
                    <el-select style='width:360px;' :filterable='true' placeholder='请选择文章'
                               v-model='formData.article_id'>
                        <el-option
                            v-for='(item,index) in articleList'
                            :key='index'
                            :label='item.label'
                            :value='item.value'
                        />
                    </el-select>
                </el-form-item>
            </el-form>
        </popup>
    </el-card>
</template>

<script lang='ts' setup name='bannerEdit'>
import type { FormInstance } from 'element-plus'
import Popup from '@/components/popup/index.vue'
import { apiBannerAdd, apiBannerEdit, apiBannerDetail } from '@/api/banner'
import { timeFormat } from '@/utils/util'
import type { PropType } from 'vue'
import request from '@/utils/request'

defineProps({
    dictData: {
        type: Object as PropType<Record<string, any[]>>,
        default: () => ({})
    }
})
const emit = defineEmits(['success', 'close'])
const formRef = shallowRef<FormInstance>()
const popupRef = shallowRef<InstanceType<typeof Popup>>()
const mode = ref('add')

const articleList = ref([])

async function initArticleList() {
    let resp = await request.post({
        url: '/article.article/getArticle'
    })
    if (!resp) resp = []
    articleList.value = resp.map((item: any) => {
        const { id, title } = item
        return { value: id, label: title }
    })
}


// 弹窗标题
const popupTitle = computed(() => {
    return mode.value == 'edit' ? '编辑' : '新增'
})

// 表单数据
const formData = reactive({
    id: '',
    title: '',
    banner_image: '',
    article_id: ''
})

// 表单验证
const formRules = reactive<any>({
    banner_image: [
        {
            required: true,
            message: '请选择图片'
        }
    ]
})


// 获取详情
const setFormData = async (data: Record<any, any>) => {
    for (const key in formData) {
        if (data[key] != null && data[key] != undefined) {
            //@ts-ignore
            formData[key] = data[key]
        }
    }


}

const getDetail = async (id: number) => {
    const data = await apiBannerDetail({
        id
    })
    setFormData(data)
}


// 提交按钮
const handleSubmit = async () => {
    await formRef.value?.validate()
    const data = { ...formData }
    mode.value == 'edit'
        ? await apiBannerEdit(data)
        : await apiBannerAdd(data)
    popupRef.value?.close()
    emit('success')
}

//打开弹窗
const open = (type = 'add') => {
    mode.value = type
    popupRef.value?.open()
    initArticleList()
}


function resetForm() {
    formData.id = ''
    formData.title = '首页上方展示图'
    formData.banner_image = ''
    formData.article_id = ''
}

// 关闭回调
const handleClose = () => {
    emit('close')
}


defineExpose({
    open,
    setFormData,
    getDetail
})
</script>
